package com.olah.ws.service;

import com.alibaba.fastjson.JSON;
import com.olah.common.entity.po.msg.system.SystemMsg;
import com.olah.common.entity.po.msg.system.SystemMsgType;
import com.olah.ws.dao.SystemMsgDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SystemMsgService {

    @Resource
    private SystemMsgDao dao;

    public List<SystemMsg> readSystemMsg(Integer userId, Boolean unreadOnly, Integer count) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", userId);
        map.put("unreadOnly", unreadOnly);
        map.put("count", count);
        return dao.readSystemMsg(map);
    }

    public SystemMsg newSystemMsg(Integer userId, Object obj, SystemMsgType type) {
        SystemMsg msg = new SystemMsg(userId, JSON.toJSONString(obj), type);
        dao.newSystemMsg(msg);
        return msg;
    }

    public void joinProject(int userId, int tagId) {
        Map<String, Integer> map = new HashMap<>();
        map.put("userId", userId);
        map.put("tagId", tagId);
        dao.joinProject(map);
    }

    public void doRead(int[] msgIds) {
        for (int msgId : msgIds) {
            doRead(msgId);
        }
    }

    public void doRead(int msgId) {
        dao.doRead(msgId);
    }

}
